// source: http://blog.thematicmapping.org/2008/04/openlayers-and-google-chart-mashup.html
var map, wms, vectors;

function z(s) {
	return s == '' ? '0' : s; 
}

function n(s) {
	return parseInt(z(s));
}

function total(feature) {
	return n(feature.attributes["AFP"]) + n(feature.attributes["A-H1N1"]) + n(feature.attributes["Cholera/Severe Diarrhea"]) 
	+ n(feature.attributes["Dengue fever"]) + n(feature.attributes["HIV/AIDs"]) + n(feature.attributes["Malaria"]) 
	+ n(feature.attributes["Measles"]) + n(feature.attributes["Pneumonia"]) + n(feature.attributes["Tuberculosis"])
	 + n(feature.attributes["Typhoid fever"]);
}

function gc_values(feature) {
	return z(feature.attributes["AFP"]) + ',' + z(feature.attributes["A-H1N1"]) + ',' + z(feature.attributes["Cholera/Severe Diarrhea"]) 
	+ ',' + z(feature.attributes["Dengue fever"]) + ',' + z(feature.attributes["HIV/AIDs"]) + ',' + z(feature.attributes["Malaria"]) 
	+ ',' + z(feature.attributes["Measles"]) + ',' + z(feature.attributes["Pneumonia"]) + ',' + z(feature.attributes["Tuberculosis"])
	+ ',' + z(feature.attributes["Typhoid fever"]);
}

function desc(feature, field) {
	return field + ": " + (feature.attributes[field] == '' ? '-': feature.attributes[field]);
}

function descAll(feature) {
	return desc(feature, "AFP") + ', ' + desc(feature, "A-H1N1") + ', ' + desc(feature, "Cholera/Severe Diarrhea") 
	+ ', ' + desc(feature, "Dengue fever") + ', ' + desc(feature, "HIV/AIDs") + ', ' + desc(feature, "Malaria") 
	+ ', ' + desc(feature, "Measles") + ', ' + desc(feature, "Pneumonia") + ', ' + desc(feature, "Tuberculosis")
	+ ', ' + desc(feature, "Typhoid fever");
}

function init(){
    var lon = 104;
    var lat = 15.3;
    var zoom = 5;
    var symbol = new Geometry('circle', 20, 1312978855);

    var context = {
        getSize: function(feature) {
            return 50;// + Math.round(symbol.getSize(total(feature)) * Math.pow(2,map.getZoom()-1));
        },
        getChartURL: function(feature) {
            var values = gc_values(feature);
            var size = 50;// + Math.round(symbol.getSize(feature.attributes["population"]) * Math.pow(2,map.getZoom()-1));
            var charturl = 'http://chart.apis.google.com/chart?cht=p&chd=t:' + values + '&chs=' + size + 'x' + size + '&chf=bg,s,ffffff00';
			return charturl;
        }
    };

    var template = {
        fillOpacity: 1.0,
        externalGraphic: "${getChartURL}",
        graphicWidth: "${getSize}",
        graphicHeight: "${getSize}",
        strokeWidth: 0
    };

    var style = new OpenLayers.Style(template, {context: context});
	var styleMap = new OpenLayers.StyleMap({'default': style, 'select': {fillOpacity: 0.7}});

    var options = {
        numZoomLevels: 7,
        controls: []  // Remove all controls
    };

    map = new OpenLayers.Map( 'olmap', options );

    wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                                    "http://labs.metacarta.com/wms/vmap0?",
                                    {layers: 'basic'},
                                    {isBaseLayer: true} );

    vectors = new OpenLayers.Layer.GML( "Internet Users", "js/prov-cases-2009.json",
                                        { format: OpenLayers.Format.GeoJSON,
                                          styleMap: styleMap,
                                          isBaseLayer: false,
                                          projection: new OpenLayers.Projection("EPSG:4326")} );

    map.addLayers([wms, vectors]);
    map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);


    map.addControl(new OpenLayers.Control.LayerSwitcher());
    map.addControl(new OpenLayers.Control.MouseDefaults());
    map.addControl(new OpenLayers.Control.PanZoomBar());

    var options = {
       hover: true,
       onSelect: serialize
    };

    var select = new OpenLayers.Control.SelectFeature(vectors, options);
    map.addControl(select);
    select.activate();
}

function serialize() {
    var Msg = "<strong>" + vectors.selectedFeatures[0].attributes["name"] + "</strong> ";
    Msg    += descAll(vectors.selectedFeatures[0]);
    document.getElementById("info").innerHTML = Msg;
}

function Geometry(symbol, maxSize, maxValue){
this.symbol = symbol;
this.maxSize = maxSize;
this.maxValue = maxValue;

this.getSize = function(value){
  switch(this.symbol) {
      case 'circle': // Returns radius of the circle
      case 'square': // Returns length of a side
          return Math.sqrt(value/this.maxValue)*this.maxSize;
      case 'bar': // Returns height of the bar
          return (value/this.maxValue)*this.maxSize;
      case 'sphere': // Returns radius of the sphere
      case 'cube': // Returns length of a side
          return Math.pow(value/this.maxValue, 1/3)*this.maxSize;
  }
}
}		
